package com.beiding.listener;

import com.beiding.dao.OrderDao;
import com.beiding.dao.SettingsDao;
import com.beiding.pojo.order.Order;
import com.beiding.pojo.payload.Payload;
import com.beiding.service.impl.MessageSender;
import com.beiding.timeout.bean.TimeShaft;
import com.beiding.utils.InformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Unlock extends TimeoutListener {

    protected Unlock(OrderDao orderDao, TimeShaft<String> timeShaft) {
        super(orderDao, timeShaft, "unlock");
    }
    private SettingsDao settings;

    @Autowired
    public void setSettings(SettingsDao settings) {
        this.settings = settings;
    }

    private MessageSender sender;

    @Autowired
    public void setSender(MessageSender sender) {
        this.sender = sender;
    }
    @Override
    public void run(String id) {
        Order order = orderDao.getInsensitive(id);

        if (judge(order, Order.LOCK)) {
            orderDao.updateField(order.getId(), "state", Order.OVERDUE);
            refund(order);
            timeShaft.delay("delete", settings.getTimeout("delete"), id);

            order.setState(Order.OVERDUE);

            sender.sendToLandlord(order.getLandlord(),update(order).msg("由于订单超时未解锁已经失效"));
            sender.sendToLandlord(order.getLandlord(),update(order).msg("由于订单超时未解锁已经失效"));

        }
    }
    private void refund(Order order) {
        System.out.println("超时未解锁自动退款");
    }
}

